package com.uenpay.im.ui.old.activity.red;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.qiyunxin.android.http.utils.StringUtils;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 创建红包
 * 
 * @author SL
 * 
 */
public class CreateRedInvokItem extends HttpInvokeItem {

	public class CreateRedInvokItemResult extends HttpInvokeResult {
		public String packetsid;
	}

	public CreateRedInvokItemResult getOutput() {
		return (CreateRedInvokItemResult) GetResultObject();
	}

	/**
	 * 创建红包
	 * 
	 * @param amount
	 * @param number
	 * @param type
	 * @param blessing
	 * @param paypassword
	 */
	public CreateRedInvokItem(String amount, String number, int type,
			String blessing, String paypassword) {
		String url = APIConfiguration.getBaseUrl()
				+ "Purse/RedPackets/createPackets?amount=" + amount
				+ "&number=" + StringUtils.encodeParams(number) + "&type="
				+ type + "&blessing=" + StringUtils.encodeParams(blessing)
				+ "&paypassword=" + paypassword + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		CreateRedInvokItemResult result = new CreateRedInvokItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONObject jsonObject = json.optJSONObject("data");
			if (jsonObject == null) {
				return result;
			}
			result.packetsid = jsonObject.optString("packetsid");
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

}
